﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using QuanLyThuoc.BUS;
namespace QuanLyThuoc.PresentionLayer
{
    public partial class frmNhaCC : Form
    {
        public int keyValue = 0;
        bool them;
        int vitri = 0;
        bool plAddMode = false;
        bool plEditMode = false;
        BindingSource dsNCC;
        private bool rAccess = true, rAdd = true, rEdit = true, rDel = true, rPrint = true;
        private int Inpost;
        private BindingManagerBase bmbList;
        public frmNhaCC()
        {
            InitializeComponent();
        }
        
        private void ResetControl()
        {
            this.txtDiachi.ReadOnly = !(this.plAddMode || this.plEditMode);
            this.txtEmail.ReadOnly = !(this.plAddMode || this.plEditMode);
            this.txtSodt.ReadOnly = !(this.plAddMode || this.plEditMode);
            this.txtIDNCC.ReadOnly = !(this.plAddMode || this.plEditMode);
            this.txtTenNCC.ReadOnly = !(this.plAddMode || this.plEditMode);

            this.cmdAdd.Image = (this.plAddMode || this.plEditMode) ? QuanLyThuoc.Properties.Resources.save24 : QuanLyThuoc.Properties.Resources.Add24;
            this.cmdEdit.Image = (this.plAddMode || this.plEditMode) ? QuanLyThuoc.Properties.Resources.undo24 : QuanLyThuoc.Properties.Resources.Edit24;

            this.cmdAdd.Text = (this.plAddMode || this.plEditMode) ? "Lưu [F2]" : "Thêm [F2]";
            this.cmdEdit.Text = (this.plAddMode || this.plEditMode)? "Hủy [F4]" : "Sửa [F4]";

            this.grdList.Enabled = !this.plAddMode;      
      
        }
       
        private void frmNhaCC_Load(object sender, EventArgs e)
        {
            //this.txtIDNCC.DataBindings.Add(new Binding("Text", this.dsNCC, this.txtIDNCC.Name.Substring(3), true, DataSourceUpdateMode.OnValidation, "1", ""));
            //this.txtTenNCC.DataBindings.Add(new Binding("Text", this.dsNCC, this.txtTenNCC.Name.Substring(3), true, DataSourceUpdateMode.OnValidation, "", ""));
            //this.txtDiachi.DataBindings.Add(new Binding("Text", this.dsNCC, this.txtDiachi.Name.Substring(3), true, DataSourceUpdateMode.OnValidation, "", ""));
            //this.txtSodt.DataBindings.Add(new Binding("Text", this.dsNCC, this.txtSodt.Name.Substring(3), true, DataSourceUpdateMode.OnValidation, "", ""));
            //this.txtEmail.DataBindings.Add(new Binding("Text", this.dsNCC, this.txtEmail.Name.Substring(3), true, DataSourceUpdateMode.OnValidation, "", ""));

           this.HienThiDanhSachNhaCungCap();
            this.ResetControl();
        }
        private void HienThiDanhSachNhaCungCap()
        {
            dsNCC = new BindingSource();
            dsNCC.DataSource = NhaCCBUS.SelectAll_NhaCC();
            grdList.DataSource = dsNCC;
           
        }
        void GanDK(DataGridViewRow r)
        {
            try
            {
                if (grdList.Rows.Count != 0)
                {
                    this.txtIDNCC.Text = r.Cells[0].Value.ToString();
                    this.txtTenNCC.Text = r.Cells[1].Value.ToString();
                    this.txtDiachi.Text = r.Cells[2].Value.ToString();
                    this.txtSodt.Text = r.Cells[3].Value.ToString();
                    this.txtEmail.Text = r.Cells[4].Value.ToString();
                }
                else
                {
                    this.txtIDNCC.Text = "";
                    this.txtTenNCC.Text = "";
                    this.txtDiachi.Text = "";
                    this.txtSodt.Text = "";
                    this.txtEmail.Text = "";
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Lỗi:" + ex.Message);
            }
        }
        public void Gan_Click()
        {
            try
            {
                if (this.grdList.SelectedRows.Count == 0)
                    return;
                DataGridViewRow rs = this.grdList.SelectedRows[0];
                GanDK(rs);
            }
            catch (Exception)
            {
                MessageBox.Show("Lỗi trong quá trình thực hiện");

            }
        }
        private void cmdAdd_Click(object sender, EventArgs e)
        {
            if (this.plAddMode || this.plEditMode)
                this.Save();
            else
                this.AddBlank();
          
            this.plAddMode = !this.plAddMode;
            this.HienThiDanhSachNhaCungCap();
            this.ResetControl();
        }

        private void gridNhaCC_Click(object sender, EventArgs e)
        {
            if (grdList.RowCount > 0)
            {
                Gan_Click();
                vitri = grdList.CurrentCell.RowIndex;
            }
        }

        private void cmdFirst_Click(object sender, EventArgs e)
        {
            dsNCC.MoveFirst();
        }

        private void cmdPrev_Click(object sender, EventArgs e)
        {
            dsNCC.MovePrevious();
        }

        private void cmdNext_Click(object sender, EventArgs e)
        {
            dsNCC.MoveNext();
        }

        private void cmdLast_Click(object sender, EventArgs e)
        {
            dsNCC.MoveLast();
        }

        private void txtIdcx_TextChanged(object sender, EventArgs e)
        {

        }

        private void gridNhaCC_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                if (this.grdList.SelectedRows.Count == 0)
                    return;
                DataGridViewRow rs = this.grdList.SelectedRows[0];
                GanDK(rs);
            }
            catch (Exception)
            {
                MessageBox.Show("Lỗi trong quá trình thực hiện");

            }
        }

        private void cmdEdit_Click(object sender, EventArgs e)
        {
            if (this.plAddMode || this.plEditMode)
            {
                this.Undo();
                this.HienThiDanhSachNhaCungCap();
            }
            else
            {
                //this.Update();
                this.Edit();
            }
            this.HienThiDanhSachNhaCungCap();
            this.ResetControl();
        }

        private void cmdDel_Click(object sender, EventArgs e)
        {
            this.Delete();
            HienThiDanhSachNhaCungCap();
        }

        public void AddBlank()
        {
            this.txtIDNCC.Text = "";
            this.txtTenNCC.Text = "";
            this.txtDiachi.Text = "";
            this.txtSodt.Text = "";
            this.txtEmail.Text = "";
            this.txtTenNCC.Focus();
            
           // HienThiDanhSachNhaCungCap();
        }
        public void TimViTri()
        {
            int MaNCC = int.Parse(txtIDNCC.Text.ToString());
            int dem = ThuocBUS.SelectIDThuoc(MaNCC).Rows.Count;
            if (vitri == grdList.RowCount)
            {
                NhaCCBUS.DeleteNhCC(MaNCC);
                this.AddBlank();
                grdList.CurrentCell = grdList.Rows[vitri - 1].Cells[0];
                grdList.Rows[vitri - 1].Selected = true;
                Gan_Click();
            }
            if (vitri == 0)
            {
                NhaCCBUS.DeleteNhCC(MaNCC);
                Gan_Click();
                grdList.CurrentCell = grdList.Rows[vitri + 1].Cells[0];
                grdList.Rows[vitri + 1].Selected = true;
                this.AddBlank();
                Gan_Click();
            }
            if (vitri > 0 && vitri < grdList.RowCount)
            {
                NhaCCBUS.DeleteNhCC(MaNCC);
                this.AddBlank();
                grdList.CurrentCell = grdList.Rows[vitri - 1].Cells[0];
                grdList.Rows[vitri - 1].Selected = true;
                Gan_Click();
            }
        }
        private void Save()
        {
            //int MaNhaCC = Convert.ToInt32(txtIDNCC.Text.ToString());
            string TenNCC = txtTenNCC.Text;
            string DiaChi = txtDiachi.Text;
            string SoDT = txtSodt.Text;
            string email = txtEmail.Text;
            if (string.IsNullOrEmpty(this.txtIDNCC.Text.Trim()))
            {
                if (KiemTraNhap())
                {
                    NhaCCBUS.InsertNhaCC(TenNCC, DiaChi, SoDT, email);
                }
                if (grdList.RowCount > 0)
                {
                    DataGridViewRow r = this.grdList.SelectedRows[0];
                    this.AddBlank();

                    Gan_Click();
                }
            }
            else
            {
                int MaNhaCC = Convert.ToInt32(txtIDNCC.Text.ToString());
                if (KiemTraNhap())
                {
                    NhaCCBUS.UpdateNhaCC(MaNhaCC, TenNCC, DiaChi, SoDT, email);
                }
            }
            
        }
      
        private void AddNew()
        {
            this.plAddMode = true;
            this.plEditMode = false;
           // this.bmbList.AddNew();
           
        }
        private void Edit()
        {
            this.plEditMode = true;
            this.plAddMode = false;
        }
        private void Undo()
        {
            //this.bmbList.CancelCurrentEdit();
            vitri = grdList.CurrentCell.RowIndex;
            this.plAddMode = false;
            this.plEditMode = false;
        }
        private void Delete()
        {
            try
            {
                int maNCC = int.Parse(txtIDNCC.Text.ToString());
                int dem = ThuocBUS.SelectIDThuoc(maNCC).Rows.Count;
                if (dem > 0)
                {
                    MessageBox.Show("Nhà cung cấp này đã có trong kho thuôc", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    if (MessageBox.Show("Bạn có muốn xóa nhà cung cấp '" + txtTenNCC.Text + "'không ??", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                    {
                        if (vitri == grdList.RowCount)
                        {
                            NhaCCBUS.DeleteNhCC(maNCC);
                            this.AddBlank();
                            grdList.CurrentCell = grdList.Rows[vitri - 1].Cells[0];
                            grdList.Rows[vitri - 1].Selected = true;
                            Gan_Click();
                        }
                        if (vitri == 0)
                        {
                            NhaCCBUS.DeleteNhCC(maNCC);
                            Gan_Click();
                            grdList.CurrentCell = grdList.Rows[vitri + 1].Cells[0];
                            grdList.Rows[vitri + 1].Selected = true;
                            this.AddBlank();
                            Gan_Click();
                        }
                        if (vitri > 0 && vitri < grdList.RowCount)
                        {
                            NhaCCBUS.DeleteNhCC(maNCC);
                            this.AddBlank();
                            grdList.CurrentCell = grdList.Rows[vitri - 1].Cells[0];
                            grdList.Rows[vitri - 1].Selected = true;
                            Gan_Click();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                // this.AddBlank();
                MessageBox.Show("Lỗi: " + ex.Message);
            }
        }
        protected override bool ProcessDialogKey(Keys keyData)
        {
            if (keyData.Equals(Keys.Escape))
            {
                this.keyValue = 0;
                if (!(this.plAddMode || this.plEditMode))
                {
                    this.Close();
                }
                else
                {
                    DialogResult dr = MessageBox.Show("Bạn có muốn đóng màn hình này không?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                    if (dr == DialogResult.Yes)
                        this.Close();
                }
            }
            else if (keyData.Equals(Keys.F2))
            {
                if ((this.plAddMode || this.plEditMode))
                    this.Save();
                else
                    this.AddNew();

                this.ResetControl();
            }
            else if (keyData.Equals(Keys.F3))
            {
                if (!(this.plAddMode || this.plEditMode))
                {
                    if (string.IsNullOrEmpty(this.txtFind.Text.Trim()))
                    {
                        this.txtFind.Select();
                        this.txtFind.SelectAll();
                    }
                    else
                    {
                        //this.Find();
                    }
                }
            }
            else if (keyData.Equals(Keys.F4))
            {
                if ((this.plAddMode || this.plEditMode))
                    this.Undo();
                else
                    this.Edit();
                this.ResetControl();
            }
            else if (keyData.Equals(Keys.Control | Keys.D))
            {
                if (!(this.plAddMode || this.plEditMode))
                    this.Delete();
                this.ResetControl();
            }
            //else if (keyData.Equals(Keys.Control | Keys.P))
            //{
            //    if (!(this.plAddMode || this.plEditMode))
            //        //this.Preview();
            //}
            return base.ProcessDialogKey(keyData);
        }

        private void cmdClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void txtFind_KeyDown(object sender, KeyEventArgs e)
        {
            //if (e.KeyCode == Keys.Enter)
            //{this
               
                
            //}
            if (txtFind.Text.Trim() == "")
            {
                this.HienThiDanhSachNhaCungCap();
            }
            else
            {
                dsNCC.DataSource = NhaCCBUS.SearchNhaCC_Condition(txtFind.Text);
                grdList.DataSource = dsNCC;
            }
        }

        private void cmdAdd_Click_1(object sender, EventArgs e)
        {

        }
        #region Kiem tra du lieu nhap
        private bool KTTenNhaCC()
        {
            if (txtTenNCC.Text.Trim() == "")
            {
                MessageBox.Show("Chưa nhập tên nhà cung cấp vui lòng nhập lại", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtTenNCC.Focus();
                return false;
            }
            return true;
        }
        private bool KTDiaChi()
        {
            if (txtDiachi.Text.Trim() == "")
            {
                MessageBox.Show("Chưa nhập địa chỉ nhà cung cấp vui lòng nhập lại", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtDiachi.Focus();
                return false;
            }
            return true;
        }
        private bool KTSoDT()
        {
            if (txtSodt.Text.Trim() == "")
            {
                MessageBox.Show("Chưa nhập số điện thoại nhà cung cấp vui lòng nhập lại", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtSodt.Focus();
                return false;
            }
            return true;
        }
        private bool KiemTraNhap()
        {
            return KTTenNhaCC() && KTDiaChi() && KTSoDT();
        }
        #endregion

    }
}
